1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.codehaus.groovy.control.customizers;
20
21 import groovy.lang.Closure;
22 import org.codehaus.groovy.ast.ClassNode;
23 import org.codehaus.groovy.classgen.GeneratorContext;
24 import org.codehaus.groovy.control.CompilationFailedException;
25 import org.codehaus.groovy.control.SourceUnit;
26 import org.codehaus.groovy.control.io.FileReaderSource;
27 import org.codehaus.groovy.control.io.ReaderSource;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class SourceAwareCustomizer extends DelegatingCustomizer {
44
45 private Closure<Boolean> extensionValidator;
46 private Closure<Boolean> baseNameValidator;
47 private Closure<Boolean> sourceUnitValidator;
48 private Closure<Boolean> classValidator;
49
50 public SourceAwareCustomizer(CompilationCustomizer delegate) {
51 super(delegate);
52 }
53
54 @Override
55 public void call(final SourceUnit source, final GeneratorContext context, final ClassNode classNode) throws CompilationFailedException {
56 String fileName = source.getName();
57 ReaderSource reader = source.getSource();
58 if (reader instanceof FileReaderSource) {
59 FileReaderSource file = (FileReaderSource) reader;
60 fileName = file.getFile().getName();
61 }
62 if (acceptSource(source) && acceptClass(classNode) && accept(fileName)) {
63 delegate.call(source, context, classNode);
64 }
65 }
66
67 public void setBaseNameValidator(final Closure<Boolean> baseNameValidator) {
68 this.baseNameValidator = baseNameValidator;
69 }
70
71 public void setExtensionValidator(final Closure<Boolean> extensionValidator) {
72 this.extensionValidator = extensionValidator;
73 }
74
75 public void setSourceUnitValidator(final Closure<Boolean> sourceUnitValidator) {
76 this.sourceUnitValidator = sourceUnitValidator;
77 }
78
79 public void setClassValidator(final Closure<Boolean> classValidator) {
80 this.classValidator = classValidator;
81 }
82
83 public boolean accept(String fileName) {
84 int ext = fileName.lastIndexOf(".");
85 String baseName = ext<0?fileName:fileName.substring(0, ext);
86 String extension = ext<0?"":fileName.substring(ext+1);
87 return acceptExtension(extension) && acceptBaseName(baseName);
88 }
89
90 public boolean acceptClass(ClassNode cnode) {
91 return classValidator == null || classValidator.call(cnode);
92 }
93
94 public boolean acceptSource(SourceUnit unit) {
95 return sourceUnitValidator==null || sourceUnitValidator.call(unit);
96 }
97
98 public boolean acceptExtension(String extension) {
99 return extensionValidator==null || extensionValidator.call(extension);
100 }
101
102 public boolean acceptBaseName(String baseName) {
103 return baseNameValidator==null || baseNameValidator.call(baseName);
104 }
105 }